package com.jrx.anytxn.param.service.rights.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.operation.PrPriceLabelReq;
import com.jrx.anytxn.param.dto.operation.PrPriceLabelRes;
import com.jrx.anytxn.param.entity.PrPriceLabel;
import com.jrx.anytxn.param.mapper.ext.ExtPrPriceLabelMapper;
import com.jrx.anytxn.param.service.rights.IPrPriceLabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 张靖烽
 * @name PrPriceLabelServiceImpl
 * @description
 * @create 2020-12-08 17:41
 **/
@Service
@CacheConfig(cacheNames = "PRICE_LABEL")
public class PrPriceLabelServiceImpl implements IPrPriceLabelService {

    private static final Logger logger = LoggerFactory.getLogger(PrPriceLabelServiceImpl.class);

    @Resource
    private ExtPrPriceLabelMapper extPrPriceLabelMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrPriceLabelRes> getAllPriceLabelByTenId(Integer pageNum, Integer pageSize, String tenantId)
            throws TxnException {
        Page<PrPriceLabelRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrPriceLabel> prPriceLabels = extPrPriceLabelMapper.selectByTenId(tenantId);
        if (prPriceLabels.isEmpty()) {
            logger.error("查询的定价标签表列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrPriceLabelRes> res = BeanMapping.copyList(prPriceLabels, PrPriceLabelRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }


    @Override
    public PrPriceLabelRes getByLabelCodeAndTenId(String labelCode, String tenantId) throws TxnException {
        if (StringUtils.isBlank(labelCode) || StringUtils.isBlank(tenantId)) {
            logger.error("标签代码或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrPriceLabel prPriceLabel = extPrPriceLabelMapper.selectByLabelCodeAndTenId(labelCode, tenantId);
        return BeanMapping.copy(prPriceLabel, PrPriceLabelRes.class);
    }


    @Override
    public PrPriceLabelRes addPriceLabel(PrPriceLabelReq prPriceLabelReq) throws TxnException {
        try {
            prPriceLabelReq.setId(segmentService.getId(Constant.PR_PRICE_LABEL));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        PrPriceLabel prPriceLabel = BeanMapping.copy(prPriceLabelReq, PrPriceLabel.class);
        /*根据索引查看是否存在此信息*/
        PrPriceLabel label = extPrPriceLabelMapper.selectByLabelCodeAndTenId(prPriceLabel.getLabelCode(), prPriceLabel.getTenantId());
        if (label != null) {
            logger.error("增加的定价标签表信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        prPriceLabel.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrPriceLabelMapper.insertSelective(prPriceLabel);
        return BeanMapping.copy(prPriceLabel, PrPriceLabelRes.class);
    }

    @CacheEvict(key = "'priceLabel_'+#prPriceLabelReq.tenantId+'_'+#prPriceLabelReq.labelCode")
    @Override
    public PrPriceLabelRes modifyPriceLabel(PrPriceLabelReq prPriceLabelReq) {
        PrPriceLabel prPriceLabel = BeanMapping.copy(prPriceLabelReq, PrPriceLabel.class);
        extPrPriceLabelMapper.updateByLabelCodeAndTenId(prPriceLabel);
        return BeanMapping.copy(prPriceLabel, PrPriceLabelRes.class);
    }

    @CacheEvict(key = "'priceLabel_'+#tenantId+'_'+#labelCode")
    @Override
    public Boolean removeByLabelCodeAndTenId(String labelCode, String tenantId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(labelCode)) {
            logger.error("标签代码或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extPrPriceLabelMapper.deleteByLabelCodeAndTenId(labelCode, tenantId);
        return a > 0;
    }

    @Cacheable(key = "'priceLabel_'+#tenantId+'_'+#labelCode")
    @Override
    public PrPriceLabel findByLabelCodeAndTenId(String labelCode, String tenantId) {
        return extPrPriceLabelMapper.selectByLabelCodeAndTenId(labelCode, tenantId);
    }
}
